package com.css.fxfzypg.modules.resultManagement.reportManagement.repository.Impl;

import com.css.fxfzypg.modules.resultManagement.reportManagement.VO.*;
import com.css.fxfzypg.modules.resultManagement.reportManagement.entity.FieldHistoryUploadReport;
import com.css.fxfzypg.modules.resultManagement.reportManagement.entity.FieldReportManagement;
import com.css.fxfzypg.modules.resultManagement.reportManagement.repository.SamplingSurveyReportRepository;
import com.css.fxfzypg.util.CreateGson;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.css.fxfzypg.web.PlatformSessionContext;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2022年12月08日  14:03
 */
@Repository
public class SamplingSurveyReportRepositoryImpl implements SamplingSurveyReportRepository {

    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Resource
    SUserService sUserService;

    @Override
    public FieldReportManagement getReportByTaskCode(String taskCode) {
        List<FieldReportManagement> list = jdbcTemplate.query("select report.* from ypg_survey_task_report report,ypg_survey_task task where report.id = task.report_id and task.del_flag='0' and task.task_num=?", new BeanPropertyRowMapper<>(FieldReportManagement.class), taskCode);
        return list.size() == 0 ? new FieldReportManagement() : list.get(0);
    }

    @Override
    public void save(FieldReportManagement reportManagement) {
        jdbcTemplate.update("insert into ypg_survey_task_report(id,default_report_name,report_path,report_create_user_id,report_create_time,model_name ) values(?,?,?,?,?,?)",
                reportManagement.getId(), reportManagement.getDefaultReportName(), reportManagement.getReportPath(), reportManagement.getReportCreateUserId(), reportManagement.getReportCreateTime(), reportManagement.getModelName());

    }

    @Override
    public void update(FieldReportManagement reportManagement) {
        String sql = "update ypg_survey_task_report set default_report_name ='" + reportManagement.getDefaultReportName() + "', report_create_user_id = '" + reportManagement.getReportCreateUserId() + "', report_create_time ='" + reportManagement.getReportCreateTime() + "',model_name='" + reportManagement.getModelName() + "' where id = '" + reportManagement.getId() + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public Map<String, Object> findParentTask(String parentId, ReportManagementVO vo, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select DISTINCT t1.create_time, t1.id,t1.task_num,task_name,t1.group_name,t1.group_leader,t1.task_district,t1.report_id from ypg_survey_task t1 left join ypg_survey_task_report t2 on t1.report_id = t2.id left join ypg_group_user t3 on t1.group_id = t3.group_id ");
        StringBuilder whereSql = new StringBuilder(" where t1.del_flag = '0' and t1.parent_id = '" + parentId + "' and (t1.task_manager_id = '" + vo.getCreateUser() + "' or t1.create_user = '" + vo.getCreateUser() + "' or t1.group_leader_id = '" + vo.getCreateUser() + "' or t3.user_id = '" + vo.getCreateUser() + "') ");//and (t2.review_status='1' or t2.review_status is null)
        StringBuilder orderSql = new StringBuilder(" order by t1.create_time desc");//reportStatus
        int firstIndex = (curPage - 1) * pageSize;

        StringBuilder str = new StringBuilder("");
        String userId = PlatformSessionContext.getUserID();
        String user = sUserService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        StringBuilder limitSql = new StringBuilder(" limit " + pageSize + " offset " + firstIndex);
        if (!PlatformObjectUtils.isEmpty(vo.getTaskNum())) {
            whereSql.append(" and t1.task_num like '%" + vo.getTaskNum() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(vo.getTaskName())) {
            whereSql.append(" and t1.task_name like '%" + vo.getTaskName() + "%'");
        }
        whereSql.append(" and (t1.task_status = '2' or t1.task_status = '1')");
        if (!PlatformObjectUtils.isEmpty(vo.getProvince()) || !PlatformObjectUtils.isEmpty(vo.getCity()) || !PlatformObjectUtils.isEmpty(vo.getCounty())) {
            //whereSql.append(" and (");
            if (!PlatformObjectUtils.isEmpty(vo.getProvince())) {
                whereSql.append(" and t1.task_district like '%" + vo.getProvince() + "%'");
            }
            if (!PlatformObjectUtils.isEmpty(vo.getCity())) {
                whereSql.append(" and t1.task_district like '%" + vo.getCity() + "%'");
            }
            //whereSql.append(") ");
            if (!PlatformObjectUtils.isEmpty(vo.getCounty())) {
                whereSql.append(" and t1.task_district like '%" + vo.getCounty() + "%'");
            }
        }
        sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(ReportManagementVO.class)).size();
        List<SamplingReportManagementVO> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(SamplingReportManagementVO.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public void updateUploadMassage(FieldReportManagement reportManagement) {
        String sql = "update ypg_survey_task_report set user_upload_report_name ='" + reportManagement.getUserUploadReportName() + "', report_upload_user_id = '" + reportManagement.getReportUploadUserId() + "', report_upload_time ='" + reportManagement.getReportUploadTime() + "',local_user_upload_report_name='" + reportManagement.getLocalUserUploadReportName() + "',report_path='" + reportManagement.getReportPath() + "' where id = '" + reportManagement.getId() + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void saveUploadMassage(FieldReportManagement reportManagement) {
        jdbcTemplate.update("insert into ypg_survey_task_report(id,user_upload_report_name,report_path,report_upload_user_id,report_upload_time,local_user_upload_report_name ) values(?,?,?,?,?,?)",
                reportManagement.getId(), reportManagement.getUserUploadReportName(), reportManagement.getReportPath(), reportManagement.getReportUploadUserId(), reportManagement.getReportUploadTime(), reportManagement.getLocalUserUploadReportName());
    }

    @Override
    public FieldReportManagement getReportById(String reportId) {
        List<FieldReportManagement> list = jdbcTemplate.query("select * from ypg_survey_task_report  where id=?", new BeanPropertyRowMapper<>(FieldReportManagement.class), reportId);
        return list.size() == 0 ? new FieldReportManagement() : list.get(0);
    }

    @Override
    public void saveHistoryUploadReport(FieldHistoryUploadReport historyUploadReport) {
        jdbcTemplate.update("insert into ypg_survey_task_history_report(id,task_id,report_name,upload_user_id,upload_user_name,upload_time,org_id,org_name,report_path,local_report_name) " +
                        "values(?,?,?,?,?,?,?,?,?,?)", historyUploadReport.getId(), historyUploadReport.getTaskId(), historyUploadReport.getReportName(), historyUploadReport.getUploadUserId(),
                historyUploadReport.getUploadUserName(), historyUploadReport.getUploadTime(), historyUploadReport.getOrgId(), historyUploadReport.getOrgName(), historyUploadReport.getReportPath(), historyUploadReport.getLocalReportName());
    }

    @Override
    public FieldReportManagement getReportByTaskId(String taskId) {
        List<FieldReportManagement> list = jdbcTemplate.query("select report.* from ypg_survey_task_report report,ypg_survey_task task where report.id = task.report_id and task.del_flag='0' and task.id=?", new BeanPropertyRowMapper<>(FieldReportManagement.class), taskId);
        return list.size() == 0 ? new FieldReportManagement() : list.get(0);
    }


    @Override
    public Map<String, Object> getHistoryReportByTaskId(String taskId, int curPage, int pageSize) {
        String sql = "select * from ypg_survey_task_history_report  where task_id='" + taskId + "' order by upload_time desc";
        int firstIndex = (curPage - 1) * pageSize;
        String limitSql = " limit " + pageSize + " offset " + firstIndex;
        List<FieldHistoryUploadReport> list = jdbcTemplate.query(sql + limitSql, new BeanPropertyRowMapper<>(FieldHistoryUploadReport.class));
        String totalSql = "select count(*) from ypg_survey_task_history_report  where task_id='" + taskId + "'";
        Integer total = jdbcTemplate.queryForObject(totalSql, Integer.class);
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public FieldHistoryUploadReport getHistoryUploadReportById(String reportId) {
        List<FieldHistoryUploadReport> list = jdbcTemplate.query("select * from ypg_survey_task_history_report  where id =?", new BeanPropertyRowMapper<>(FieldHistoryUploadReport.class), reportId);
        return list.size() == 0 ? null : list.get(0);
    }

    @Override
    public List<FieldReportManagementVO> getFieldReport() {
        String sql = "select a.*,b.task_name,b.task_district from ypg_survey_task_report a left join ypg_survey_task b on a.id=b.report_id where b.del_flag='0'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FieldReportManagementVO.class));
    }

    @Override
    public Map<String, Object> getReportListByTaskId(String taskId, int curPage, int pageSize) {
        String sql = "select * from ypg_survey_task_history_report  where task_id='" + taskId + "' order by upload_time desc";
        int firstIndex = (curPage - 1) * pageSize;
        String limitSql = " limit " + pageSize + " offset " + firstIndex;
        List<FieldHistoryUploadReportVo> list = jdbcTemplate.query(sql + limitSql, new BeanPropertyRowMapper<>(FieldHistoryUploadReportVo.class));
        String totalSql = "select count(*) from ypg_survey_task_history_report  where task_id='" + taskId + "'";
        Integer total = jdbcTemplate.queryForObject(totalSql, Integer.class);
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }
}
